package goulu.bloggy.com.Activities;

import java.util.ArrayList;

import goulu.bloggy.com.R;
import goulu.bloggy.com.Connections.PostMysqlConnection;
import goulu.bloggy.com.Helpers.PostHelper;
import goulu.bloggy.com.Models.Post;
import goulu.bloggy.com.Models.PostAdapter;
import goulu.bloggy.com.Models.User;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class BrowseBlogsActivity extends ListActivity implements OnClickListener
{
	private ArrayList<Post> postsList;
	private String searchedUser;
	private PostAdapter postAdapter;
	private EditText searchInput;
	private TextView searchedUsernameText;
	
    Handler postHandler = new Handler()
    {

		@Override
		public void handleMessage(Message msg) 
		{
			if(msg.what == PostMysqlConnection.GET_PUBLISHED_POSTS)
			{
				Log.d("ViewPosts.handleMessage", "got posts, add them to list");
				searchedUsernameText.setText("Blogposts of " + searchedUser);
				searchInput.clearFocus();
				postsList.clear();
				System.out.println(postsList.isEmpty());
				postsList.addAll((ArrayList<Post>)msg.obj);
				postAdapter.notifyDataSetChanged();
			}
			else if(msg.what == PostMysqlConnection.ERROR)
			{
				searchedUsernameText.setText("");
				postsList.clear();
				postAdapter.notifyDataSetChanged();
				Toast.makeText(getApplicationContext(), (String)msg.obj, Toast.LENGTH_LONG).show();
			}
		}
    	
    };
	
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
    	Log.d("ViewPostsActivity.onCreate()", "oncreatee");
        super.onCreate(savedInstanceState);
        postsList = new ArrayList<Post>();
        setContentView(R.layout.browseblogview);
        this.setupView();
        //this.getPostsFromDB();

    }

	private void getPostsFromDB() 
	{
		Log.d("browseblogsactivity.getPostsFromDB", "getting posts from db by username = " + searchedUser);
		PostMysqlConnection connection = new PostMysqlConnection(postHandler, PostMysqlConnection.GET_PUBLISHED_POSTS, searchedUser);
		Thread t = new Thread(connection);
		t.start();
	}

	private void setupView() 
	{
        postAdapter = new PostAdapter(getApplicationContext(), postsList);
        ListView postList = (ListView)findViewById(android.R.id.list);
        postList.setAdapter(postAdapter);
        
        searchedUsernameText = (TextView)findViewById(R.id.browseblogSearchedUsernameText);
        searchInput = (EditText)findViewById(R.id.browseblogUserInput);
		
        Button viewblogBT = (Button)findViewById(R.id.browseblogViewUserBT);
        viewblogBT.setOnClickListener(this);
        
        Button backBT = (Button)findViewById(R.id.browseblogbackBT);
        backBT.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) 
	{
		switch (v.getId()) 
		{
		case R.id.browseblogViewUserBT:
			searchedUser = searchInput.getText().toString();
			this.getPostsFromDB();
			break;
		case R.id.browseblogbackBT:
			finish();
			break;
		}	
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) 
	{
		super.onListItemClick(l, v, position, id);
		Post post = postsList.get(position);
		Intent intent = new Intent(getApplicationContext(), ViewSinglePostActivity.class);
		intent.putExtra("post", post);
		intent.putExtra("editable", false);
		startActivity(intent);
	}
}